# -*- coding: utf-8 -*-
import scrapy
import time
import spiderlist.items

class MovieSpider(scrapy.Spider):
    name = 'movie'
    allowed_domains = ['zuidazy1.com/']
    custom_settings = {
        'RETRY_TIMES': 3,  # 重复请求的次数
        'DOWNLOAD_DELAY': 0.3,  # 下载器间隔时间
        # 'spiderlist.pipelines.JsonCreatePipeline':200,  # 开启json文件保存  数字任意
    }

    def start_requests(self):
        # 遍历所有页面
        for i in range(930):
            url = f'http://www.zuidazy1.com/?m=vod-index-pg-{i}.html'
            yield scrapy.Request(url, dont_filter=True, callback=self.parse_video)

    def parse_video(self, response):
        # 当前页的电影名称
        moviename = response.css('.xing_vb4 a::text').extract()
        #获取当前页面所有 电影链接
        movie_url = response.css('.xing_vb4 a::attr(href)').extract()  
        for page in movie_url:
            url=f'http://www.zuidazy1.com'+page
            yield scrapy.Request(url, dont_filter=True, callback=self.detail_video)


    def detail_video(self,response):
        #处理电影详情页数据
        movie_title=response.css('.vodInfo h2::text').extract_first().strip()
        movie_img=response.css('.vodImg img::attr(src)').extract_first().strip()
        movie_info=response.css('.vodplayinfo::text').extract_first().strip()
        #电影类型
        movie_type=response.css('.vodinfobox ul li:nth-child(4) span::text').extract_first().strip()
        #电影地区
        movie_city=response.css('.vodinfobox ul li:nth-child(5) span::text').extract_first().strip()
        #电影更新时间
        movie_update=response.css('.vodinfobox ul li:nth-child(9) span::text').extract_first().strip()
        #电影zuidam3u8 播放类型 是一个数组
        movie_m3u8=response.css('#play_1 li::text').extract()
        #电影zuidall 播放类型 是一个数组
        movie_zuidal=response.css('#play_2 li::text').extract()
        #迅雷下载
        movie_xunlei=response.css('#down_1 li::text').extract()

        try:
            item=spiderlist.items.movieSearchItem()
            item['movie_title'] = movie_title
            item['movie_img'] = movie_img
            item['movie_type'] = movie_type
            item['movie_city'] = movie_city
            item['movie_update'] = movie_update
            item['movie_info'] = movie_info
            item['movie_m3u8'] = movie_m3u8
            item['movie_zuidal'] = movie_zuidal
            item['movie_xunlei'] = movie_xunlei
            yield item
        except:
            print('error')
   

    def close(spider, reason):
        #爬虫用时
        print('用时：', time.time() - spider.crawler.engine.start_time)

